<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <!-- <el-form-item label="itemTypeId">
                    <el-input v-model="searchObj.itemTypeId" placeholder="itemTypeId" />
                </el-form-item>
                <el-form-item label="itemPic">
                    <el-input v-model="searchObj.itemPic" placeholder="itemPic" />
                </el-form-item> -->
      <el-form-item label="商品名称">
        <el-input v-model="searchObj.itemName" placeholder="请输入商品名称" />
      </el-form-item>
      <!-- <el-form-item label="itemSpecification">
                    <el-input v-model="searchObj.itemSpecification" placeholder="itemSpecification" />
                </el-form-item>
                <el-form-item label="itemModel">
                    <el-input v-model="searchObj.itemModel" placeholder="itemModel" />
                </el-form-item>
                <el-form-item label="itemUnit">
                    <el-input v-model="searchObj.itemUnit" placeholder="itemUnit" />
                </el-form-item>
                <el-form-item label="itemPrice">
                    <el-input v-model="searchObj.itemPrice" placeholder="itemPrice" />
                </el-form-item> -->
      <el-button type="primary" icon="el-icon-search" @click="fetchData()">
        查询
      </el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 列表 -->
    <el-table :data="list" border stripe>
      <el-table-column label="#" width="50">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <!-- <el-table-column prop="itemTypeId" label="itemTypeId" />
                <el-table-column prop="itemPic" label="itemPic" /> -->
      <el-table-column prop="itemPic" label="商品图片" min-width="100%">
        <template slot-scope="scope">
          <div class="demo-fit">
            <div class="block">
              <el-avatar
                shape="square"
                :size="100"
                :src="scope.row.itemPic"
              ></el-avatar>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="itemName" label="商品名称" />
      <el-table-column prop="itemSpecification" label="商品规格" />
      <el-table-column prop="itemModel" label="商品型号" />
      <el-table-column prop="itemUnit" label="单位" />
      <el-table-column prop="itemPrice" label="单价" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-link :href="'http://127.0.0.1/' + scope.row.id + '.html'">
            预览
            <i class="el-icon-view el-icon--right"></i>
          </el-link>
          <router-link
            :to="'/Item/edit/' + scope.row.id"
            style="margin-right:5px;"
          >
            <el-button type="primary" size="mini" icon="el-icon-edit">
              修改
            </el-button>
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeById(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[10, 15, 20]"
      style="padding: 30px 0; "
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>
<script>
import itemApi from '@/api/Item'
export default {
  data() {
    return {
      list: null, // 数据列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {} // 查询条件
    }
  },
  created() {
    // 当页面加载时获取数据
    this.fetchData()
  },
  methods: {
    fetchData() {
      itemApi
        .getPageList(this.page, this.limit, this.searchObj)
        .then(response => {
          this.list = response.data.pageModel.list
          this.total = response.data.pageModel.total
        })
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.limit = size
      this.fetchData()
    },
    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page
      this.fetchData()
    },
    // 重置表单
    resetData() {
      this.searchObj = {}
      this.fetchData()
    },
    removeById(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          itemApi.removeById(id).then(response => {
            this.$message.success(response.message)
            this.fetchData()
          })
        })
        .catch(error => {
          this.$message.info('取消删除')
        })
    }
  }
}
</script>
